package com.estar.assistsurvy.activity;

import java.io.Serializable;
import java.util.List;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.entity.RequestTask;
import com.estar.assistsurvy.entity.ResponseOrder;
import com.estar.assistsurvy.entity.grabList;
import com.estar.assistsurvy.thread.TaskSupervisionThread;
import com.estar.assistsurvy.utils.ExitUtils;
import com.estar.assistsurvy.utils.MightypleUtil;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class SearchTargetcarActivity extends BaseActivity implements OnClickListener{

	private EditText input_info;
	private Button cancel,search;
	private InputMethodManager imm;
	private SharedPreferences prefer;
	private String deviceUUID,uUID;
	private String caseNumber="",targetCar="",taskState="",startTime="",endTime="";
	private Intent intent;
	
	private Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case -1:
				showShortToast((String) msg.obj);
				break;
				
			case 0:
				ResponseOrder task=new ResponseOrder();
				task=(ResponseOrder) msg.obj;
				if(task.getCode().equals("1")){
					List<grabList> grabList=task.getGrabList();
					if(grabList==null||grabList.size()==0){
						showShortToast("您的搜索结果为空!");
					}else{
						intent.putExtra("grabList", (Serializable) grabList);
						setResult(0, intent);
						finish();
					}
					
				}else if(task.getCode().equals("0")){
					showShortToast(task.getMessages());
				}else if(task.getCode().equals("2")){
					ExitUtils.Unlogin(SearchTargetcarActivity.this, task.getMessages());
				}
				break;

			default:
				break;
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_searchcar);
		initView();
	}
	
	private void initView(){
		cancel=find(R.id.cancel);
		cancel.setOnClickListener(this);
		input_info=find(R.id.input_info);
		search=find(R.id.search);
		search.setOnClickListener(this);
		
		prefer=getSharedPreferences("user", MODE_PRIVATE);
		uUID=prefer.getString("uuid", "");
		deviceUUID=MightypleUtil.getDeviceId(SearchTargetcarActivity.this);
		
		intent=getIntent();
//		if(intent.getExtras().getInt("key")==0){
//			name="报案号";
//		}else if(intent.getExtras().getInt("key")==1){
//			name="车牌号";
//		}
//		input_info.setHint("请输入"+name+"…");
		imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		 /*隐藏软键盘*/
		imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
		
		switch (v.getId()) {
		case R.id.cancel:
			finish();
			break;
			
		case R.id.search:
//			if(intent.getExtras().getInt("key")==0){
//				caseNumber=input_info.getText().toString().trim();
//				intent.putExtra("caseNumber", caseNumber);
//				setResult(0, intent);
//			}else if(intent.getExtras().getInt("key")==1){
//				targetCar=input_info.getText().toString().trim();
//				intent.putExtra("targetCar", targetCar);
//				setResult(1, intent);
//			}
//			finish();
			targetCar=input_info.getText().toString().trim();
			if(targetCar.equals("")){
				showShortToast("请输入您要搜索的车牌号!");
				return;
			}
			RequestTask requestTask=new RequestTask(deviceUUID, uUID, caseNumber, targetCar, taskState, startTime, endTime);
			new TaskSupervisionThread(SearchTargetcarActivity.this, handler, requestTask).start();
			break;

		default:
			break;
		}
		
	}
	
}
